var userName
var passWord
localStorage.removeItem("token")
localStorage.removeItem("id")
// localStorage.removeItem("gods_id")
// 账号
$('[name="a1"]').on("input", function () {
    userName = $(this)[0].value
    var a1 = /^[a-z0-9]\w{4,11}$/
    var thiss = this
    Color(thiss,a1,userName)
})
$('[name="a1"]').on("blur", function () {
    var thiss= this
    Out(thiss,userName)
})
// 密码
$('[name="a2"]').on("input", function () {
    passWord = $(this)[0].value
    var a2 = /\w{6,12}/
    var thiss= this
    Color(thiss,a2,passWord)
})
$('[name="a2"]').on("blur", function () {
    var thiss= this
    Out(thiss,passWord)
})
// 方法1
function Color(thiss,ze,ele) {
    if (ele === ``) {
        $(this).css({
            border: "1px solid pink"
        })
    } else if (ze.test(ele)) {
        $(thiss).css({
            border: "1px solid green"
        })
    } else if (!ze.test(ele)) {
        $(thiss).css({
            border: "1px solid red"
        })
    }
}
// 方法2
function Out(thisss,ele){
    if (ele === `` || ele === undefined) {
        $(thisss).css({
            border: "1px solid red"
        })
    }
}
$("form").on("submit",function(){
    if($('[name="a1"]').css("border-color")==="rgb(0, 128, 0)" && $('[name="a2"]').css("border-color")==="rgb(0, 128, 0)"){
        console.log("登录"+"用户名"+userName +"密码"+passWord);
        promiseAjax({
            url:"http://localhost:8888/users/login",
            method:"post",
            headers:{
                "content-type":"application/x-www-form-urlencoded"
            },
            data:{
                username:userName,
                password:passWord,
            }
        }).then(function(res){
            console.log(res);
            if(res.code===1){
                var {message,token,user:{id}} = res
                alert(message)
                // location.href = `home.html?id=${id}&token=${token}`
                localStorage.setItem("id",id)
                localStorage.setItem("token",token)
                if(localStorage.getItem("gods_id")){
                    location.href = `data.html`
                }else{  
                    location.href = `home.html`
                }
                
            }else{
                alert(res.message)
            }
        })
    }else{
        alert("请检查输入内容")
    }
    return false
})
$(".register").hover(function(){
    $(this).css({
        color:"#e31436"
    })
},function(){
    $(this).css({
        color:"#666"
    })
})
$(".register").on("click",function(){
    location.href = "register.html"
})